package models

import "gorm.io/gorm"

// MaActivity [...]
type MaActivity struct {
	ID            string `gorm:"autoIncrement:true;primaryKey;column:id;type:int;not null" json:"id" json_old:"ID"`
	CouponBatchID string `gorm:"column:coupon_batch_id;type:string;size:20;comment:优惠码批次id" json:"coupon_batch_id"`
	Title         string `gorm:"column:title;type:varchar(30);not null;comment:活动标题" json:"title" json_old:"Title"`
	Content       string `gorm:"column:content;type:text;not null;comment:控件内容" json:"content" json_old:"Content"`
	ImagePath     string `gorm:"column:image_path;type:varchar(256);default:null;comment:图片地址" json:"image_path" json_old:"ImagePath"`
	Color         string `gorm:"column:color;type:varchar(30);default:null;comment:颜色值" json:"color" json_old:"Color"`
	Status        int    `gorm:"column:status;type:tinyint(1);not null;default:0;comment:0开启 1关闭" json:"status" json_old:"Status"`
	MerchantID    string `gorm:"column:merchant_id;type:int;size:64;default:1;comment:所属商户id" json:"merchant_id"`
	CreatedAt     int64  `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
	UpdatedAt     int64  `gorm:"column:updated_at;autoUpdateTime;type:int;size:64;comment:修改时间" json:"updated_at"`
	Deleted       int    `gorm:"column:deleted;type:tinyint(1);default:null;default:0;comment:0正常 1删除" json:"deleted" json_old:"Deleted"`

	//优惠券属性字段
	TemplateName   string            `gorm:"column:template_name;type:varchar(128);default:null;comment:优惠券模板名称" json:"template_name"`
	CouponBindType CouponBindTypeDef `gorm:"column:coupon_bind_type;type:varchar(20);default:null;comment:优惠券类型" json:"coupon_bind_type"`
	GoodsID        int64             `gorm:"column:goods_id;type:bigint;default:0;comment:商品id" json:"goods_id,string"`
	GoodsClassID   int64             `gorm:"column:goods_class_id;type:bigint;default:0;comment:商品标签id" json:"goods_class_id,string"`
	OnlineEventID  int64             `gorm:"column:online_event_id;type:bigint;default:0;comment:活动id" json:"online_event_id,string"`

	//优惠券总数、已用数量、库存数量
	TotalCouponCode int `gorm:"column:total_coupon_code;type:int;default:0;comment:优惠码总数" json:"total_coupon_code"`
	UsedCouponCode  int `gorm:"column:used_coupon_code;type:int;default:0;comment:已使用优惠码数" json:"used_coupon_code"`
	StockCouponCode int `gorm:"-" json:"stock_coupon_code"`

	CodeStartTime int64 `gorm:"column:code_start_time;type:bigint;default:0;comment:码开始时间" json:"code_start_time"`
	CodeEndTime   int64 `gorm:"column:code_end_time;type:bigint;default:0;comment:码结束时间" json:"code_end_time"`

	PageStartTime int64 `gorm:"column:page_start_time;type:bigint;default:0;comment:页面开始时间" json:"page_start_time"`
	PageEndTime   int64 `gorm:"column:page_end_time;type:bigint;default:0;comment:页面结束时间" json:"page_end_time"`

	CodeBackgroundImg string `gorm:"column:code_background_img;type:varchar(1024);default:null;comment:发码背景图片" json:"code_background_img"`
}

// TableName get sql table name.获取数据库表名
func (m MaActivity) TableName() string {
	return "ma_activity"
}

func (m *MaActivity) AfterFind(tx *gorm.DB) error {
	m.StockCouponCode = m.TotalCouponCode - m.UsedCouponCode
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &MaActivity{})
}

// 卡券绑定类型定义
type CouponBindTypeDef string

const (
	CouponBindTypeGoods      CouponBindTypeDef = "goods"       // 关联商品
	CouponBindTypeGoodsClass CouponBindTypeDef = "goods_class" // 关联商品标签
	CouponBindTypeActivity   CouponBindTypeDef = "activity"    // 关联活动
)
